import { Button, Input, Form, message } from 'antd';
import React from 'react';
import styles from './index.less';
import { history, Link } from 'umi';
import request from 'umi-request';

function Login() {
  const onFinish = (values) => {
    console.log('Success:', values);
    if (values.user_name === 'admin' && values.a_pw === 'admin') {
      history.push('/admin');
      message.success({
        content: '欢迎你管理员',
        key: 'global_key',
        duration: 2,
      });
    } else {
      sessionStorage.setItem('account', window.JSON.stringify(values));
      request
        .post('http://123.60.20.244:8080/atm/account/login', {
          params: values,
        })
        .then((response) => {
          console.log(response);
          if (response.code === 0) {
            message.success({
              content: '登陆成功',
              key: 'global_key',
              duration: 2,
            });
            history.push({
              pathname: '/home/info',
            });
            return Promise.reject();
          } else if (response.code === 1) {
            message.error({
              content: response.message,
              key: 'global_key',
              duration: 2,
            });
            return Promise.reject();
          } else if (response.code === 2) {
            message.error({
              content: response.message,
              key: 'global_key',
              duration: 2,
            });
            return Promise.reject();
          } else if (response.code === 3) {
            message.error({
              content: response.message,
              key: 'global_key',
              duration: 2,
            });
          } else if (response.code === 4) {
            message.error({
              content: response.message,
              key: 'global_key',
              duration: 2,
            });
          }
        });
    }
  };

  return (
    <div className={styles.card}>
      <div className={styles.loginTitle}>用户登录</div>
      <Form name="basic" onFinish={onFinish}>
        <Form.Item
          name="user_name"
          rules={[
            {
              required: true,
              message: '请填写登录账号',
            },
          ]}
        >
          <Input placeholder="账号/手机号 admin" />
        </Form.Item>

        <Form.Item
          name="a_pw"
          rules={[
            {
              required: true,
              message: '请填写登录密码',
            },
          ]}
        >
          <Input.Password placeholder="请输入密码 admin" />
        </Form.Item>

        <Form.Item
          extra={<div style={{ marginTop: 8 }}>忘记密码请联系管理员处理</div>}
        >
          <Button type="primary" htmlType="submit" block>
            登 录
          </Button>
          <Link to="/register">注册</Link>
        </Form.Item>
      </Form>
    </div>
  );
}

export default Login;
